package cn.itcast.order.service.impl;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.feign.client.UserClient;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.common.ThreadLocalUtil;
import cn.itcast.hmall.dto.order.OrderReqDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.order.Order;
import cn.itcast.hmall.pojo.order.OrderDetail;
import cn.itcast.hmall.pojo.order.OrderLogistics;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.pojo.user.User;
import cn.itcast.order.mapper.OrderDetailMapper;
import cn.itcast.order.mapper.OrderLogisticsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.itcast.order.service.OrderService;
import cn.itcast.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;

/**
* @author T50
* @description 针对表【tb_order】的数据库操作Service实现
* @createDate 2022-06-22 14:23:24
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    @Override
    public String createOrder(OrderReqDTO orderReqDTO) {
        Item item = itemClient.selectOne(orderReqDTO.getItemId().toString());
        Long totalFee = item.getPrice()*orderReqDTO.getNum();
        Order order = new Order();
        order.setTotalFee(totalFee);
        order.setPaymentType(3);
        order.setStatus(1);
        Long userId = ThreadLocalUtil.getUserId();
        order.setUserId(userId);
        save(order);
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(orderReqDTO.getItemId());
        orderDetail.setNum(orderReqDTO.getNum());
        orderDetail.setName(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetailMapper.insert(orderDetail);
        Address address = userClient.findByAddressId(orderReqDTO.getAddressId().toString());
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogisticsMapper.insert(orderLogistics);
        int resStock = (item.getStock())-(orderReqDTO.getNum());
        if(resStock>0){
            item.setStock(resStock);
            itemClient.changeUpdate(item);
        }
        return order.getId().toString();
    }

    @Override
    public ResultDTO checkPassword(Long orderId, Map<String,String> keys) {
        Order order = getById(orderId);
        if(order.getStatus()==1){
            Long userId = ThreadLocalUtil.getUserId();
            User user = userClient.findUserBySystemId(userId);
            String res = keys.get("password");
            String safePw = DigestUtils.md5DigestAsHex(res.getBytes());
            if(user.getPassword().equals(safePw)){
                long money = (order.getTotalFee()) / 100;
                Long balance = user.getBalance();
                long remainMoney = balance-money;
                user.setBalance(remainMoney);
                user.setId(userId);
                userClient.changeMonye(user);
                order.setStatus(2);
                updateById(order);
                return ResultDTO.ok();
            }else{
                return ResultDTO.error("密码错误");
            }
        }else {
            return ResultDTO.error("您已付过此订单");
        }
    }
}




